package leetcode.code2217;

import leetcode.IDebug;
import leetcode.helper.H;

public class Solution implements IDebug {
	public long[] kthPalindrome(int[] queries, int intLength) {
		int len = queries.length;
		long[] ans = new long[len];
		if (intLength == 1) {
			for (int i = 0; i < ans.length; i++) {
				ans[i] = queries[i] >= 10 ? -1 : queries[i];
			}
			return ans;
		}
		int base = (int) Math.pow(10, (intLength >> 1) - 1);
		int range = (int) Math.pow(10, intLength >> 1);
		if ((intLength & 1) == 1) {
			for (int i = 0; i < ans.length; i++) {
				int no = queries[i] - 1;
				int find = base + no / 10;
				ans[i] = find >= range ? -1 : oddPalinrome(find, no % 10);
			}
		} else {
			for (int i = 0; i < ans.length; i++) {
				int no = queries[i] - 1;
				int find = base + no;
				ans[i] = find >= range ? -1 : evenPalinrome(find);
			}
		}
		return ans;
	}

	public long evenPalinrome(int num) {
		long mirror = 0;
		long ans = num;
		while (num > 0) {
			mirror = mirror * 10 + num % 10;
			ans *= 10;
			num /= 10;
		}
		return ans + mirror;
	}

	public long oddPalinrome(int num, int no) {
		long mirror = 0;
		long ans = num * 10 + no;
		long mid = 1;
		while (num > 0) {
			mirror = mirror * 10 + num % 10;
			num /= 10;
			mid *= 10;
		}
		return ans * mid + mirror;
	}

	@Override
	public void debug4() {
//		this.kthPalindrome(H.str2array3("[1,2,3,4,5,90]"), 2);

	}

	@Override
	public void debug3() {
		H.compare(H.str2array3("[1,2,3,4,5,6,7,8,9,-1,-1,-1,-1,-1,9,8]"),
				this.kthPalindrome(H.str2array3("[1,2,3,4,5,6,7,8,9,10,11,12,13,14,9,8]"), 1));
		H.compare(H.str2array3("[2,-1,8,-1,-1,-1,-1,9,7,6]"),
				this.kthPalindrome(H.str2array3("[2,201429812,8,520498110,492711727,339882032,462074369,9,7,6]"), 1));
		H.compare(H.str2array3("[101,111,121,131,141,999]"), this.kthPalindrome(H.str2array3("[1,2,3,4,5,90]"), 3));

	}

	@Override
	public void debug2() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug1() {
		// TODO Auto-generated method stub

	}

	public static void main(String[] args) {
		Solution so = new Solution();
		so.debug1();
		so.debug2();
		so.debug3();
		so.debug4();

	}

}
